//
//  SystemDataBase.m
//  AAIM
//
//  Created by awh on 2020/1/5.
//  Copyright © 2020年 AAIM. All rights reserved.
//

#import "SystemDataBase.h"
#import "BTMessageModel.h"
@interface SystemDataBase ()
@property(nonatomic, strong) FMDatabaseQueue *sysGroupDBQueue; //群通知
@property(nonatomic, copy)   NSString *sysGroupTableName;
@property(nonatomic, strong) FMDatabaseQueue *sysMessageDBQueue; //系统消息
@property(nonatomic,   copy) NSString *sysMessageTableName;
@property(nonatomic, strong) FMDatabaseQueue *sysFriendMsgDBQueue; //好友系统消息
@property(nonatomic,   copy) NSString *sysFriendMsgTableName;
@end
@implementation SystemDataBase

static SystemDataBase *single = nil;

+ (instancetype)shareSystemDataBase {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        single = [[self alloc] init];
    });
    return single;
}
- (instancetype)init {
    if (self = [super init]) {
        [self initDataBase];
    }
    return self;
}
-(void)initDataBase
{
    NSString *dbPath  =[CacheTool readUserDefaults:@"APPDATABASE_KEY"];
    self.sysGroupDBQueue = [[FMDatabaseQueue alloc] initWithPath:dbPath];
    self.sysFriendMsgDBQueue = [[FMDatabaseQueue alloc] initWithPath:dbPath];
}
- (void)createTable {
    [self.sysGroupDBQueue inDatabase:^(FMDatabase *db) {
        NSString *createTableSql = [NSString stringWithFormat:@"create table if not exists %@ ('fuuid' TEXT, 'type_id' INTEGER ,'gId' TEXT, 'head' TEXT , 'ishow' TEXT , 'msg' TEXT  , 'msg_id' TEXT , 'msgtype' INTEGER , 'nickname' TEXT , 'rfuuid' TEXT, 'gidorfuuid' TEXT , 'type' TEXT , 'createtime' TEXT, 'addfriendState' INTEGER)", self.sysGroupTableName];
        [db executeUpdate:createTableSql];
    }];
    
    [self.sysFriendMsgDBQueue inDatabase:^(FMDatabase *db) {
        NSString *createTableSql = [NSString stringWithFormat:@"create table if not exists %@ ('fuuid' TEXT, 'type_id' INTEGER ,'gId' TEXT, 'head' TEXT , 'ishow' TEXT , 'msg' TEXT  , 'msg_id' TEXT , 'msgtype' INTEGER , 'nickname' TEXT , 'rfuuid' TEXT, 'gidorfuuid' TEXT , 'type' TEXT , 'createtime' TEXT, 'addfriendState' INTEGER)", self.sysFriendMsgTableName];
        [db executeUpdate:createTableSql];
    }];
    
//    [self.sysMessageDBQueue inDatabase:^(FMDatabase *db) {
//        NSString *createTableSql = [NSString stringWithFormat:@"create table if not exists %@ ('user_id' TEXT, 'groupHead' TEXT, 'groupName' TEXT , 'isMeCreate' INTEGER)", self.sysMessageTableName];
//        [db executeUpdate:createTableSql];
//    }];
}
#pragma mark - 插入数据 增
-(void)insertSystemData:(BTMessageModel*)systemModel
{
    [self.sysGroupDBQueue inDatabase:^(FMDatabase * _Nonnull db) {
        NSString *tableNameHead = [NSString stringWithFormat:@"insert into '%@' ", self.sysGroupTableName];
        NSString *content = [NSString stringWithFormat:@"(fuuid, type_id, gId ,head, ishow, msg, msg_id, msgtype, nickname , rfuuid, gidorfuuid, type ,createtime,addfriendState) values (?, ?, ? , ?, ?, ?, ?, ?, ?, ?, ?, ? ,?,?)"];
        NSString *sql = [NSString stringWithFormat:@"%@%@", tableNameHead, content];
        [db executeUpdate:sql,systemModel.fuuid,@(systemModel.type_id),systemModel.gId,systemModel.head,systemModel.ishow,systemModel.msg,systemModel.msg_id,@(systemModel.msgtype),systemModel.nickname,systemModel.rfuuid,systemModel.gidorfuuid,systemModel.type,systemModel.createtime,@(systemModel.addfriendState)];
    }];
}
-(void)insertFriendSystemData:(BTMessageModel*)systemFriendModel
{
    [self.sysFriendMsgDBQueue inDatabase:^(FMDatabase * _Nonnull db) {
        NSString *tableNameHead = [NSString stringWithFormat:@"insert into '%@' ", self.sysFriendMsgTableName];
        NSString *content = [NSString stringWithFormat:@"(fuuid, type_id, gId ,head, ishow, msg, msg_id, msgtype, nickname , rfuuid, gidorfuuid, type ,createtime,addfriendState) values (?, ?, ? , ?, ?, ?, ?, ?, ?, ?, ?, ? ,?,?)"];
        NSString *sql = [NSString stringWithFormat:@"%@%@", tableNameHead, content];
        [db executeUpdate:sql,systemFriendModel.fuuid,@(systemFriendModel.type_id),systemFriendModel.gId,systemFriendModel.head,systemFriendModel.ishow,systemFriendModel.msg,systemFriendModel.msg_id,@(systemFriendModel.msgtype),systemFriendModel.nickname,systemFriendModel.rfuuid,systemFriendModel.gidorfuuid,systemFriendModel.type,systemFriendModel.createtime,@(systemFriendModel.addfriendState)];
    }];
}
#pragma mark - 查
- (NSArray<BTMessageModel *> *)queryGroupNoticeMessages
{
    NSMutableArray *conversations = [NSMutableArray new];
    [self.sysGroupDBQueue inDatabase:^(FMDatabase *db) {
        NSString *sql = [NSString stringWithFormat:@"select * from %@ ;", self.sysGroupTableName];
        FMResultSet *result = [db executeQuery:sql];
        while ([result next]) {
            [conversations addObject:[self dataWith:result]];
        }
    }];
    if (conversations.count > 0) {
        return conversations;
    } else {
        return nil;
    }
    
}
- (NSArray<BTMessageModel *> *)queryFriendsNoticeMessages
{
    NSMutableArray *conversations = [NSMutableArray new];
    [self.sysFriendMsgDBQueue inDatabase:^(FMDatabase *db) {
        NSString *sql = [NSString stringWithFormat:@"select * from %@ ;", self.sysFriendMsgTableName];
        FMResultSet *result = [db executeQuery:sql];
        while ([result next]) {
            [conversations addObject:[self dataWith:result]];
        }
    }];
    if (conversations.count > 0) {
        return conversations;
    } else {
        return nil;
    }
}
#pragma mark - 改
-(void)updateApplyGroupState:(BTMessageModel*)msg
{
    [self.sysGroupDBQueue inDatabase:^(FMDatabase *db) {
      NSString *sql = [NSString stringWithFormat:@"update %@ set addfriendState = %ld where gidorfuuid = %@ ;", self.sysGroupTableName, msg.addfriendState, msg.gidorfuuid];
     [db executeUpdate:sql];
   }];
}

-(BTMessageModel*)dataWith:(FMResultSet *)result {
    BTMessageModel * model =[BTMessageModel new];
    model.fuuid=[result stringForColumn:@"fuuid"];
    model.type_id=[result intForColumn:@"type_id"];
    model.gId=[result stringForColumn:@"gId"];
    model.head=[result stringForColumn:@"head"];
    model.ishow=[result stringForColumn:@"ishow"];
    model.msg=[result stringForColumn:@"msg"];
    model.msg_id=[result stringForColumn:@"msg_id"];
    model.msgtype=[result intForColumn:@"msgtype"];
    model.nickname=[result stringForColumn:@"nickname"];
    model.rfuuid=[result stringForColumn:@"rfuuid"];
    model.gidorfuuid=[result stringForColumn:@"gidorfuuid"];
    model.type=[result stringForColumn:@"type"];
    model.createtime=[result stringForColumn:@"createtime"];
    model.addfriendState=[result intForColumn:@"addfriendState"];

    return model;
}

#pragma mark - getter

- (NSString *)sysGroupTableName {
    UserModel * model_user= [[LoginManager shareLoginManager] getLoginCustomer];
    return  [NSString stringWithFormat:@"sysGroupData_%@", model_user.fuuid];
}
- (NSString *)sysMessageTableName {
    UserModel * model_user= [[LoginManager shareLoginManager] getLoginCustomer];
    return  [NSString stringWithFormat:@"sysMessageData_%@", model_user.fuuid];
}
- (NSString *)sysFriendMsgTableName {
    UserModel * model_user= [[LoginManager shareLoginManager] getLoginCustomer];
    return  [NSString stringWithFormat:@"sysFriendMessageData_%@", model_user.fuuid];
}
@end
